package com.aoip.telemetry.telemetry.configureui.business.computemodel.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aoip.telemetry.telemetry.configureui.business.computemodel.service.ComputeModelService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.entity.quality.ComputeModelEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/5/1 16:53
 */
@RestController
public class ComputeModelController extends BaseController {
    @Resource
    private ComputeModelService service;

    /**
     * 计算模型分页列表
     *
     * @param entity 计算模型
     * @return com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo
     * @author chenbai
     * @date 2022/4/29
     **/
    @GetMapping("/v1/GET/compute/model/page")
    public TableDataInfo page(ComputeModelEntity entity) {
        startPage();
        List<ComputeModelEntity> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 计算模型不分页列表@author chenbai
     *
     * @param entity 计算模型
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @date 2022/4/29
     **/
    @GetMapping("/v1/GET/compute/model/list")
    public AjaxResult list(ComputeModelEntity entity) {
        List<ComputeModelEntity> list = service.list(entity);
        return AjaxResult.success(list);
    }

    /**
     * 获取单个计算模型
     *
     * @param modelId 计算模型主键
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/4/29
     **/
    @GetMapping("/v1/GET/compute/model/st/{modelId}")
    public AjaxResult getRelModelStListByModelId(@PathVariable String modelId) {
        return AjaxResult.success(service.getRelModelStListByModelId(modelId));
    }

    /**
     * 获取单个计算模型
     *
     * @param id 计算模型主键
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/4/29
     **/
    @GetMapping("/v1/GET/compute/model/{id}")
    public AjaxResult computeModel(@PathVariable String id) {
        return AjaxResult.success(service.detail(id));
    }

    /**
     * 新增计算模型
     **/
    @Log(title = "计算模型", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/compute/model")
    public AjaxResult add(@Validated @RequestBody ComputeModelEntity entity) {
        return toAjax(service.insert(entity));
    }

    @Log(title = "新增计算模型与测站关联关系", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/compute/model/st")
    public AjaxResult addRelModelSt(@Validated @RequestBody JSONObject params) {
        List<String> stCodes = JSON.parseArray(JSON.toJSONString(params.get("stCodes")), String.class);
        String modelId = JSON.parseObject(JSON.toJSONString(params.get("modelId")), String.class);
        return toAjax(service.insertRelModelSt(stCodes, modelId));
    }

    @Log(title = "计算模型启用状态变更", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/compute/model/enable")
    public AjaxResult enableStatusChange(@Validated @RequestBody ComputeModelEntity entity) {
        return toAjax(service.enableStatusChange(entity));
    }

    /**
     * 修改计算模型
     */
    @Log(title = "计算模型", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/compute/model")
    public AjaxResult edit(@Validated @RequestBody ComputeModelEntity entity) {
        return toAjax(service.update(entity));
    }

    /**
     * 初始化计算模型缓存数据到redis中，数据格式（id, ComputeModelEntity）,（计算模型主键，计算模型实体）
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/compute/model/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }

    /**
     * 删除计算模型
     */
    @Log(title = "计算模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/compute/model/{ids}")
    public AjaxResult remove(@PathVariable long[] ids) {
        service.delete(ids);
        return success();
    }
}
